package com.imessage.action.business.othertask;

import com.bean.*;
import com.imessage.action.BaseAction;
import com.imessage.type.*;
import com.imessage.vo.TaskVo;
import io.ebean.Expr;
import io.ebean.Expression;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

public class OtherTaskBaseAction extends BaseAction {

    protected final String datagrid = "datagrid-task-filter";

    protected void setExpressionList(List<Expression> expressionList, TaskVo taskVo) {
        if (StringUtils.isNotBlank(taskVo.getName())) {
            expressionList.add(Expr.contains("name", taskVo.getName()));
        }
        if (StringUtils.isNotBlank(taskVo.getVmMac())) {
            expressionList.add(Expr.contains("DeviceVirtual.virtualMac", taskVo.getVmMac()));
        }
        if (StringUtils.isNotBlank(taskVo.getMsgType())) {
            expressionList.add(Expr.eq("type", taskVo.getMsgType()));
        }
        if (StringUtils.isNotBlank(taskVo.getCmd())) {
            expressionList.add(Expr.eq("cmd", taskVo.getCmd()));
        }else{
            expressionList.add(Expr.ne("cmd", BusinessTaskCmd.send_batch_msg.getCode()));
        }
        if (StringUtils.isNotBlank(taskVo.getState())) {
            expressionList.add(Expr.eq("state", taskVo.getState()));
        }
    }


    protected void setModelAndView(ModelAndView modelAndView, BusinessTaskDetail businessTaskDetail) {

        if (businessTaskDetail.getDeviceClient() == null) businessTaskDetail.setDeviceClient(new DeviceClient());
        if (businessTaskDetail.getDeviceVirtual() == null) businessTaskDetail.setDeviceVirtual(new DeviceVirtual());
        if (businessTaskDetail.getBusinessWuma() == null) businessTaskDetail.setBusinessWuma(new BusinessWuma());
        if (businessTaskDetail.getBusinessApple() == null) businessTaskDetail.setBusinessApple(new BusinessApple());

        modelAndView.addObject("businessTaskDetailForm", businessTaskDetail);
        modelAndView.addObject("statusList", BusinessTaskState.values());

        modelAndView.addObject("clientList", baseService.findList(DeviceClient.class, Expr.isNotNull("id")));
        modelAndView.addObject("vmList", baseService.findList(DeviceVirtual.class, Expr.isNotNull("id")));
        modelAndView.addObject("appleIdList", baseService.findList(BusinessApple.class, Expr.isNotNull("id")));
        modelAndView.addObject("wumaList", baseService.findList(BusinessWuma.class, Expr.isNotNull("id")));
        modelAndView.addObject("cmdList", BusinessTaskCmd.values());
        modelAndView.addObject("msgTypeList", BusinessTaskType.values());
    }


    protected void setTaskDetail(TaskVo taskVo, BusinessTaskDetail taskDetail) {
        copyProperties(taskVo, taskDetail);
        if (StringUtils.isNotBlank(taskVo.getState())) {
            taskDetail.setState(BusinessTaskState.fromValue(taskVo.getState()));
        }
        if (StringUtils.isNotBlank(taskVo.getClientId())) {
            taskDetail.setDeviceClient(baseService.findObject(DeviceClient.class, Expr.eq("id", taskVo.getClientId())));
        }
        if (StringUtils.isNotBlank(taskVo.getVmId())) {
            taskDetail.setDeviceVirtual(baseService.findObject(DeviceVirtual.class, Expr.eq("id", taskVo.getVmId())));
        }
        if (StringUtils.isNotBlank(taskVo.getWuma())) {
            taskDetail.setBusinessWuma(baseService.findObject(BusinessWuma.class, Expr.eq("id", taskVo.getWuma())));
        }
        if (StringUtils.isNotBlank(taskVo.getAppleId())) {
            taskDetail.setBusinessApple(baseService.findObject(BusinessApple.class, Expr.eq("id", taskVo.getAppleId())));
        }
        if (StringUtils.isNotBlank(taskVo.getCmd())) {
            taskDetail.setCmd(BusinessTaskCmd.fromValue(taskVo.getCmd()));
        }
        if (StringUtils.isNotBlank(taskVo.getMsgType())) {
            taskDetail.setType(BusinessTaskType.fromValue(taskVo.getMsgType()));
        }
    }
}
